package com.tencent.dao;

import com.tencent.pojo.Student;
import com.tencent.util.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;

import java.util.List;

/**
 * @author: Fame
 * @date: 2022-06-20 15:45
 * @description:
 */
public class StudentDAOImpl extends BaseDAO implements StudentDAO{

    @Override
    public int insertStu(Student student) {
        String sql = "insert into student values(null,?,?,?,?)";
        return update(sql,student.getName(),student.getAge(),student.getPhone(),student.getAddress());
    }

    @Override
    public List<Student> getAll(Integer pageNo) {
        String sql = "select id,t_name name,t_age age,t_phone phone,t_address address from student limit ?,7" ;
        List<Student> list = queryForList(Student.class, sql, (pageNo - 1) * 7);
        return list;
    }

    @Override
    public Student getById(Integer id) {
        String sql = "select id,t_name name,t_age age,t_phone phone,t_address address from student where id = ?";
        return queryForBean(Student.class,sql,id);
    }

    @Override
    public int updateStu(Student student) {
        String sql = "update student set t_name=?,t_age=?,t_phone=?,t_address=? where id = ?";
        return update(sql,student.getName(),student.getAge(),student.getPhone(),student.getAddress(),student.getId());
    }

    @Override
    public int delStu(Integer id) {
        String sql = "delete from student where id = ?";
        return update(sql,id);
    }

    @Override
    public int getCount() {
        String sql = "select count(*) from student";
        Integer pageNo = ((Long) super.queryForSingeValue(sql,null)).intValue();
        return pageNo;
    }

    @Override
    public int getCount(String keyword) {
        String sql = "select count(*) from student where t_name like ?";
        Integer pageNo = ((Long) super.queryForSingeValue(sql,"%"+keyword+"%")).intValue();
        return pageNo;
    }

    @Override
    public List<Student> getAll(String keyword, Integer pageNo) {
        String sql = "select id,t_name name,t_age age,t_phone phone,t_address address from student where t_name like ? limit ?,7" ;
        List<Student> list = queryForList(Student.class, sql, "%"+keyword+"%",(pageNo - 1) * 7);
        return list;
    }
}
